﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;
using SPSolutions.Samples.RibbonApp.WebControls;

namespace SPSolutions.Samples.RibbonApp.Layouts.RibbonApp
{
    /// <summary>
    /// Represent a page using a custom ribbon
    /// </summary>
    public partial class RibbonPage : RibbonPageBase, IPostBackEventHandler
    {
        /// <summary>
        /// Load the page
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            InitializeRibbon();
        }

        /// <summary>
        /// Initialize the ribbon
        /// </summary>
        private void InitializeRibbon()
        {
            // Get the ribbon
            SPRibbon current = SPRibbon.GetCurrent(this.Page);

            if (current != null)
            {
                // Setup the ribbon
                current.Minimized = false;
                current.CommandUIVisible = true;
                current.MakeTabAvailable("Ribbon.RibbonApp.RibbonPage");
                current.InitialTabId = "Ribbon.RibbonApp.RibbonPage";
                
                // Register scripts
                ScriptLink.RegisterScriptAfterUI(this.Page, "SP.Runtime.js", false, true);
                ScriptLink.RegisterScriptAfterUI(this.Page, "SP.js", false, true);

                // Get the ribbon script manager
                RibbonScriptManager ribbonScriptManager = new RibbonScriptManager("RibbonApp/");
                ribbonScriptManager.RegisterInitializeFunction(this, "InitPageComponent", "RibbonApp.UI.js", false, "RibbonApp.UI.RibbonAppPageComponent.initialize()");

                // Create ribbon commands
                List<IRibbonCommand> commands = new List<IRibbonCommand>();
                commands.Add(new SPRibbonCommand("Ribbon.RibbonApp.RibbonPage"));
                commands.Add(new SPRibbonCommand("Ribbon.RibbonApp.RibbonPage.NewGroup"));
                commands.Add(new SPRibbonCommand("Ribbon.RibbonApp.RibbonPage.NewGroup.New", "ShowPopupDialog('/_layouts/RibbonApp/RibbonPopupPage.aspx')", "true"));
                commands.Add(new SPRibbonCommand("Ribbon.RibbonApp.RibbonPage.PlayfulGroup"));
                commands.Add(new SPRibbonCommand("Ribbon.RibbonApp.RibbonPage.PlayfulGroup.Laugh", "alert('Laugh, laugh, laugh...')", "true"));
                commands.Add(new SPRibbonPostBackCommand("Ribbon.RibbonApp.RibbonPage.PlayfulGroup.Giggle", this, "Giggle", null, "true"));
                commands.Add(new SPRibbonCommand("Ribbon.RibbonApp.RibbonPage.PlayfulGroup.JSEnableDisable", "alert('JSEnableDisable Clicked')", "IsJSEnabled()"));

                // Register ribbon scripts
                SPRibbonScriptManager spRibbonScriptManager = new SPRibbonScriptManager();
                spRibbonScriptManager.RegisterGetCommandsFunction(this, "getGlobalCommands", commands);
                spRibbonScriptManager.RegisterCommandEnabledFunction(this, "commandEnabled", commands);
                spRibbonScriptManager.RegisterHandleCommandFunction(this, "handleCommand", commands);
            }
        }

        /// <summary>
        /// Handle postback events
        /// </summary>
        /// <param name="eventArgument"></param>
        public void RaisePostBackEvent(string eventArgument)
        {
            // Parse the ribbon events
            SPRibbonPostBackEvent ribbonEvents = SPRibbonPostBackCommand.DeserializePostBackEvent(eventArgument);

            // Validate page
            Page.Validate();

            // Handle send event
            if (string.Equals(ribbonEvents.Id, "Giggle", StringComparison.InvariantCultureIgnoreCase))
            {
                Giggle_Click(this, new EventArgs());
            }
        }

        /// <summary>
        /// Ribbon app post back event button clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void Giggle_Click(object sender, EventArgs e)
        {
            // Invalid page input
            if (!Page.IsValid)
            {
                return;
            }

            // Do something
            Response.Write("Giggle, giggle, giggle...");
        }
    }
}
